﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyFisheryListControl : YuanlinUserControl,
        IReplyConverter<CompanyFisheryReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList companyFisherys;
        public string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        public string CategoryName
        {
            get
            {
                string str = "";
                switch (tradeType)
                {
                    case"0":
                        str = ResourceManager.GetString("Sell");
                        break;
                    case "1":
                        str = ResourceManager.GetString("JingPin");
                        break;
                    case "2":
                        str = ResourceManager.GetString("Buy");
                        break;
                    case "3":
                        str = ResourceManager.GetString("JiShou");
                        break;
                }
                return str;
            }
        }

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                requestBinders.Add(RequestHelper.BuildGetCompanyFisheryListRequest(this, user.EntityId.Value, "", tradeType, pager.PageIndex, pager.PageSize));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }

            list.DataSource = companyFisherys;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(companyFisherys.TotalCount);
                currentPage.TotalRecords = (companyFisherys.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteCompanyProduct(int companyProductId)
        {
            CompanyFishery companyFishery = BusinessManager.GetCompanyFishery(companyProductId);
            if (companyFishery != null)
            {
                BusinessManager.DeleteCompanyFishery(companyProductId);
                SiteUrls.Instance().DeleteFile(companyFishery);

                if (companyFisherys.Count == 0)
                {
                    switch (tradeType)
                    {
                        case "0":
                            {
                                user.Fishery = "";
                            }
                            break;
                        case "2":
                            {
                                user.FisheryQiuGou = "";
                            }
                            break;
                        case "1":
                            {
                                user.FisheryJingPin = "";
                            }
                            break;
                        case "3":
                            {
                                user.FisheryJiShou = "";
                            }
                            break;
                    }
                   UserManager.UpdateUser(user);
                }
                AquacultureFeedbackListRequest request1 = new AquacultureFeedbackListRequest();
                request1.CompanyFisheryId = companyProductId;
                request1.CategoryType = categoryType;
                request1.PageIndex = 0;
                request1.PageSize = 1;
                EntityList feedbacks = BusinessManager.GetAquacultureFeedbackList(RequestHelper.BuildGetAquacultureFeedbackListRequest(request1));

                if (feedbacks.Count == 1)
                {
                    AquacultureFeedback feedback = feedbacks[0] as AquacultureFeedback;
                    BusinessManager.DeleteAquacultureFeedback(feedback.EntityId.Value);
                }
            }
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteCompanyProducts(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteCompanyProduct(entityId); 
                }
            }
            return 0;
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void UpdateCompanyProduct(int companyProductId)
        {
            CompanyFishery companyProduct = BusinessManager.GetCompanyFishery(companyProductId);
            if (companyProduct != null)
            {
                companyProduct.UpdateDate.Value = DateTime.Now;
                BusinessManager.UpdateCompanyFishery(companyProduct);
            }
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int UpdateCompanyProducts(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    UpdateCompanyProduct(entityId);
                }
            }
            return 0;
        }

        #endregion

        #region IReplyConverter<CompanyFisheryReply,GetListOperation> Members

        public void ConvertReply(CompanyFisheryReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyFisherys = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}